package com.cyf.app;

import com.cyf.app.network.bean.*;
import com.cyf.app.service.AppPayService;
import com.cyf.wechat.network.bean.WechatBusinessRsp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@Controller
@RestController
@Api("APP支付相关api")
@RequestMapping(value = "app")
public class AppPayHttpController {

    private static final Logger logger = LoggerFactory.getLogger(AppPayHttpController.class);

    @Autowired
    private AppPayService appPayService;

    @ApiOperation(value = "APP微信付款统一下单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "orderNo", value = "订单号（getFee接口的返回字段）", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "订单类型：0-支付停车费 1-支付广告费", required = false, dataType = "int", paramType = "query")
    })
    @PostMapping(value = "/wxpay/unifiedOrder")
    public AppWxpayUnifiedOrderRsp wxpayUnifiedOrder(@RequestParam String token,
                                                     @RequestParam String orderNo,
                                                     @RequestParam(required = false, defaultValue = "0") int type,
                                                     HttpServletRequest request) {
        String ip = request.getRemoteHost();
        logger.info("APP（{}）请求微信付款的统一下单", ip);
        AppWxpayUnifiedOrderReq req = new AppWxpayUnifiedOrderReq();
        req.setToken(token);
        req.setOrderNo(orderNo);
        req.setType(type);
        req.setIp(ip);
        return appPayService.wxpayUnifiedOrder(req);
    }

    @ApiOperation(value = "APP微信付款后查询支付结果")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "orderNo", value = "订单号（getFee接口的返回字段）", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "transactionId", value = "交易订单号（微信同步返回字段）", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "订单类型：0-支付停车费 1-支付广告费", required = false, dataType = "int", paramType = "query")
    })
    @PostMapping(value = "/wxpay/queryOrder")
    public AppWxpayQueryOrderRsp wxpayQueryOrder(@RequestParam String token,
                                                 @RequestParam(required = false) String orderNo,
                                                 @RequestParam(required = false) String transactionId,
                                                 @RequestParam(required = false, defaultValue = "0") int type) {
        AppWxpayQueryOrderReq req = new AppWxpayQueryOrderReq();
        req.setToken(token);
        req.setOrderNo(orderNo);
        req.setTransactionId(transactionId);
        req.setType(type);
        return appPayService.wxpayQueryOrder(req);
    }

    @ApiOperation(value = "APP支付宝付款下单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "orderNo", value = "订单号（getFee接口的返回字段）", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "订单类型：0-支付停车费 1-支付广告费", required = false, dataType = "int", paramType = "query")
    })
    @PostMapping(value = "/alipay/unifiedOrder")
    public AppAlipayUnifiedOrderRsp alipayUnifiedOrder(@RequestParam String token,
                                                       @RequestParam String orderNo,
                                                       @RequestParam(required = false, defaultValue = "0") int type) {
        AppAlipayUnifiedOrderReq req = new AppAlipayUnifiedOrderReq();
        req.setToken(token);
        req.setOrderNo(orderNo);
        req.setType(type);
        return appPayService.alipayUnifiedOrder(req);
    }

    @ApiOperation(value = "APP支付宝付款后查询支付结果")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "orderNo", value = "订单号（getFee接口的返回字段）", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "transactionId", value = "交易订单号（支付宝同步返回字段）", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "订单类型：0-支付停车费 1-支付广告费", required = false, dataType = "int", paramType = "query")
    })
    @PostMapping(value = "/alipay/queryOrder")
    public AppAlipayQueryOrderRsp alipayQueryOrder(@RequestParam String token,
                                                   @RequestParam(required = false) String orderNo,
                                                   @RequestParam(required = false) String transactionId,
                                                   @RequestParam(required = false, defaultValue = "0") int type) {
        AppAlipayQueryOrderReq req = new AppAlipayQueryOrderReq();
        req.setToken(token);
        req.setOrderNo(orderNo);
        req.setTransactionId(transactionId);
        req.setType(type);
        return appPayService.alipayQueryOrder(req);
    }


    @ApiOperation(value = "微信月卡充值付款统一下单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "orderNo", value = "订单号（getFee接口的返回字段）", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "订单类型：0-支付停车费 1-支付广告费", required = false, dataType = "int", paramType = "query")
    })
    @PostMapping(value = "/wxpay/monthlyOrder")
    public AppWxpayUnifiedOrderRsp monthlyOrder(@RequestParam String orderNo, @RequestParam String code,
                                                @RequestParam(required = false, defaultValue = "1") int type,
                                                HttpServletRequest request) {
        String ip = request.getRemoteHost();
        logger.info("月卡充值（{}）请求微信付款的统一下单", ip);
        AppWxpayUnifiedOrderReq req = new AppWxpayUnifiedOrderReq();
        req.setOrderNo(orderNo);
        req.setType(type);
        req.setIp(ip);
        req.setCode(code);
        return appPayService.wxpayMonthlyOrder(req);
    }


    @ApiOperation(value = "微信月卡付款支付结果查询")
    @PostMapping(value = "/wxpay/monthlyOrderResult")
    public WechatBusinessRsp monthlyOrderResult(@RequestParam String orderNo, HttpServletRequest request) {
        logger.info("月卡充值结果查询（{}）", orderNo);
        return appPayService.monthlyOrderResult(orderNo);
    }

}
